package per.chenyl.framework.persistence.wrapper;


import per.chenyl.framework.persistence.common.constant.PersistenceConstant;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class QueryWrapper<T> extends AbstractWrapper<T> {
    private boolean useSelect = false;

    public QueryWrapper() {
    }

    public QueryWrapper(T entity) {
        super(entity);
    }

    @Override
    public Wrapper<T> orderBy(String column, boolean desc) {
        condition.append("order by ").append(column).append(" ");
        if (desc) {
            condition.append("desc ");
        }
        return this;
    }

    @Override
    public Wrapper<T> batchOrderBy(HashMap<String, Boolean> columns) {
        condition.append("order by ");
        Set<Map.Entry<String, Boolean>> entries = columns.entrySet();
        entries.forEach(en -> {
            condition.append(en.getKey()).append(" ");
            condition.append(en.getValue() ? "desc," : ",");
        });
        condition = new StringBuilder(condition.substring(0, condition.length() - 1));
        return this;
    }

    @Override
    public Wrapper<T> select(String colums) {
        useSelect = true;
        condition.delete(0, condition.length());
        condition.append("select ").append(colums).append(" from ").append(PersistenceConstant.TABLE_PLACEHOLDER)
                .append(" ").append("where 1=1 ");
        return this;
    }

    @Override
    public Wrapper<T> limit(int coumt) {
        condition.append("limit ").append(coumt);
        return this;
    }

    @Override
    public boolean isUseSelect() {
        return useSelect;
    }
}
